package br.com.sigmah.controller.mbeans;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

import br.com.sigmah.controller.facade.LoginFacade;
import br.com.sigmah.controller.facade.UtilsFacade;
import br.com.sigmah.domain.entity.Curso;
import br.com.sigmah.domain.entity.Usuario;
import br.com.sigmah.domain.repository.CursoDataManager;

@ManagedBean
@SessionScoped
public class LoginController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LoginController(){
		
	}
	
	@ManagedProperty("#{utilsFacade}")
	private UtilsFacade utilsFacade;
	
	@ManagedProperty("#{loginFacade}")
	private LoginFacade loginFacade;
	
	private Usuario usuario;
//	@Autowired
//	private CursoDAO cursoDao;
	
	private Curso curso;
	
	private String nome;

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}
	
	public String login(){
//		utilsFacade.utilsToString();
		
	//	cursoDao.save(curso);
		return "login";
	}
	
	@PostConstruct
	public void init(){
		this.usuario = new Usuario();
        SecurityContext context = SecurityContextHolder.getContext();
        if (context instanceof SecurityContext){
            Authentication authentication = context.getAuthentication();
            if (authentication instanceof Authentication){
                usuario.setUsername(((User)authentication.getPrincipal()).getUsername());
            }
        }
	}
	
	public String verificaUsuario(){
		
		loginFacade.verificarUsuario(usuario);
		return "home";

	}

	public UtilsFacade getUtilsFacade() {
		return utilsFacade;
	}

	public void setUtilsFacade(UtilsFacade utilsFacade) {
		this.utilsFacade = utilsFacade;
	}

	public Curso getCurso() {
		return curso;
	}

	public void setCurso(Curso curso) {
		this.curso = curso;
	}

	public LoginFacade getLoginFacade() {
		return loginFacade;
	}

	public void setLoginFacade(LoginFacade loginFacade) {
		this.loginFacade = loginFacade;
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}
	
	
	
}
